﻿#include "Star.h"
//#include "Global.h"
#include "Resource.h"

Star::Star(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State, int Direction) : Object(x, y)
{
	this->SetID(ID);
	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\item_32.png"), 150, 0);

	_xOld = _x;
	_yOld = _y;

	_vx = 0;
	_vy = 0;
	_accelY = 0;


	container = NULL;
	isCheckedContainer = false;

	//object_State = IS_IDLE;
	object_State = Object_State;
	direction = Direction;
	if (object_State == IS_MOVING)
	{
		_sprite->SetFrame(3,4);
		if (direction == 1) //sang trai
		{
			_vx=-0.200;
		} 
		else if (direction == 2) //sang phai
		{
			_vx=0.200;
		}
	} 
	else if (object_State == IS_IDLE)
	{
		_sprite->SetFrame(7,7);	//frame an
	}
}

Star::~Star()
{
	/*if (_sprite != NULL)
	{
	delete _sprite;
	}*/
}

void Star::Render()
{
	if (object_State != IS_REMOVE)
	{
		_sprite->Render(_x, _y);
	}

}

OBJECT_TYPE Star::GetTypeObject()
{

	return OBJECT_LAND;
}

void Star::ResetRect()
{
	_rectBond.left = _x;
	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
	_rectBond.top = _y;
	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
}

void Star::Update(list<Object*>* staObjs, list<Object*>* dynObjs, DxInput* input, float gameTime)
{
	switch(object_State)
	{
	case IS_IDLE:
		CheckObjects(staObjs,dynObjs);
		break;
	case IS_ACTIVING:
		_sprite->SetFrame(3,4);

		_x+=_vx*gameTime;
		_y+=_vy*gameTime+1/2*_accelY*gameTime*gameTime;
		if(_y< this->GetBond().bottom - 32-5)
			ChangeState(IS_MOVING);

		break;
	case IS_MOVING:

		Move(gameTime,staObjs,dynObjs);
		break;
	}


	_sprite->Update(gameTime);
	ResetRect();
}

void Star::CheckObjects(list<Object*>* staObjs,list<Object*>* dynObjs)
{

	list<Object*>::iterator i;
	Object* obj;
	if(isCheckedContainer==false)
		for(i=staObjs->begin();i!=staObjs->end();i++)
		{
			obj=*i;
			if(obj->GetID()==21||obj->GetID()==25) //gach:21, gach dau hoi: 25
				if(Collision::RectVsRect(this->GetBond(),obj->GetBond()))
				{
					container=obj;
					break;
				}
		}

		isCheckedContainer=true;
}

void Star::Move(float TPF,list<Object*>* staObjs,list<Object*>* dynObjs)
{
	Object* obj;
	//Theo phương x
	_x+=_vx*TPF;
	ResetRect();
	list<Object*>::iterator i;
	for(i=staObjs->begin();i!=staObjs->end();i++)
	{
		obj=*i;
		//if(obj->GetID()>=20&&obj->GetID()<=29) //Nếu là thùng, đá , gạch...
		if (obj->GetTypeObject() == OBJECT_LAND)
			if(Collision::RectVsRect(this->GetBond() , obj->GetBond()) == true)	
			{
				_vx=-_vx;
				_x=_xOld;
			}
	}
	//Theo phương y
	_accelY = GRAVITY;
	_vy+=_accelY*TPF;
	_y+=_vy*TPF+1.0/2*_accelY*TPF*TPF;
	ResetRect();
	for(i=staObjs->begin();i!=staObjs->end();i++)
	{
		obj=*i;
		//if(obj->ID>=20&&obj->ID<=28) //Nếu là thùng, đá , gạch...
		if (obj->GetTypeObject() ==OBJECT_LAND)
			if(Collision::RectVsRect(this->GetBond() , obj->GetBond())==true)	
			{
				_accelY=0;
				_vy=-1.2;
				_y=_yOld;
			}
	}

	_xOld=_x;
	_yOld=_y;

	if (_vx >= 0)
	{
		direction = 2;
	} 
	else
	{
		direction = 1;
	}
}

void Star::ChangeState(int state)
{
	object_State = state;
	switch(state)
	{
	case IS_ACTIVING:

		_vy=-4.00;

		break;
	case IS_MOVING:
		_vy=0;

		_vx=0.200;
		break;
	case IS_READY:
		_y=this->GetBond().bottom - 32; 
		break;

	case IS_REMOVE:

		break;
	}
}